/*
 * Conexao.java
 *
 * Created on 11 de Junho de 2005, 19:53
 */
package camada.bancodedados.conexao;

/**
 *
 * @author Marcos Pisching
 */
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class Conexao {

    private static Connection con;
    private static Statement stm;

    public Conexao() {
    }

    public static void conectar() {

        try {

            String usuario = null;
            String senha = null;
            String servidor = null;
            String porta = null;
            String banco = null;

            Scanner scanner = new Scanner(new FileReader("C:\\Seloma\\banco.bat")).useDelimiter("\\n");

            while (scanner.hasNext()) {
                servidor = (scanner.next());
                porta = (scanner.next());
                banco = (scanner.next());
                usuario = (scanner.next());
                senha = (scanner.next());
            }

            String driver = "com.mysql.jdbc.Driver";
            //String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
            String bancoDados = "jdbc:mysql://" + servidor.trim() + ":" + porta.trim() + "/" + banco.trim();
            //String bancoDados = "jdbc:sqlserver://ICARO_KLINGER//KLINGER;databaseName=SGE;";
            String user = usuario.trim();
            String pass = senha.trim();

            Class.forName(driver);
            con = DriverManager.getConnection(bancoDados, user, pass);
            stm = con.createStatement();

        } catch (ClassNotFoundException e) {
            System.out.print(e.getMessage());
        } catch (SQLException e) {
            System.out.print(e.getMessage());
        } catch (FileNotFoundException e) {
            System.out.print(e.getMessage());
        }
    }

    public static void encerrar() {
        try {
            con.close();

            System.out.print("FINALIZADA CONEXAO SQL");

        } catch (SQLException e) {
            System.out.print("ERRO ENCERRAR CONEXAO SQL");
        }

    }

    public static Statement getStm() {
        return stm;
    }

    public static Connection getCon() {
        if (con == null) {
            conectar();
            System.out.print("INICIADA CONEXAO SQL");
        }
        return con;
    }

    public static void setStm(Statement s) {
        stm = s;
    }

    public static void setCon(Connection c) {
        con = c;
    }
}
